import 'dart:async';

import 'package:flutter_hi_cache/flutter_hi_cache.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/pay_model/access_info_query_model.dart';
import 'package:yysk_app/model/pay_model/payment_order_pay_query_model.dart';
import 'package:yysk_app/net/api/pay_url.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/pages/login/login_controller.dart';
import 'package:yysk_app/pages/main/main_controller.dart';
import 'package:yysk_app/pages/main/main_page.dart';
import 'package:yysk_app/pages/main/mine/mine_page_controller.dart';
import 'package:yysk_app/pages/nft_pages/pay_pages/confirm_payment/confirm_payment_controller.dart';
import 'package:yysk_app/routes/global_controller.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:oktoast/oktoast.dart';

class ResultPaymentZhifuController extends GetxController {
  final txt = '返回'.obs;
  final buyNow = '0'.obs;
  final payNo = ''.obs;
  final detail  = Rx<PaymentOrderPayQueryModel?>(null);
    Timer? _timer;
      var accessToken = ''.obs; //cahs数据token
       var uuid = ''.obs; //uuid
  @override
  // ignore: unnecessary_overrides
  void onInit() {
     super.onInit();
    final params = Get.parameters ?? {};
    final args = Get.arguments ?? {};
    try {
      buyNow.value = params['buyNow'] ?? args['buyNow'];
      payNo.value = params['outTradeNo'] ?? args['outTradeNo'];
    } catch (e) {
      buyNow.value = '0';
    }
    // accessToken.value = Get.parameters['accessToken'] ?? '-1';
    //  uuid.value = '${new DateTime.now().millisecondsSinceEpoch}';
      payResult();
       _timer = Timer.periodic(Duration(seconds: 3), (timer) {
         payResult();
       });
     
  }

  @override
  // ignore: unnecessary_overrides
  void onClose() {
    super.onClose();
    _timer?.cancel();
  }

  callbackmain() {
    try {
      Get.until((route) => Get.currentRoute.contains(MainPage.path));
    } catch (e) {
      Get.offAllNamed(MainPage.path);
    }
  }
  Future<void> payResult()async {
    String userToken = '';
    try{
      ConfirmPaymentController confirmController = Get.find();
      userToken = confirmController.token.value;
    }catch(e){
      userToken = HiCache.getInstance().get(LoginController.boardingPassToken)??getToken();      
    }
    await http.netFetch<PaymentOrderPayQueryModel>(
      PayUrl.orderPayQuery,
      method: Method.post,
      params: {
        'userToken': userToken,
        "outTradeNo":payNo.value
      },
      success: (result) {
        if (result.code == 200) {
         detail.value = result.data;
         if( result.data?.status=='SUCCESS'){
             _timer?.cancel();
         }
        }
      },
    );
  }
    //获取权限信息查询
  // Future<dynamic> orderInfo() async {
  //   try {
  //     await http.netFetch<AccessInfoQueryModel>(
  //       PayUrl.accessInfoQuery,
  //       method: Method.post,
  //       params: {'accessToken': accessToken.value, 'uuid': uuid.value},
  //       success: (result) {
  //         if (result.code == 200) {
  //           GlobalController globalController = Get.find();
  //           token.value = result.data!.userToken!;

  //           globalController.saveXPayToken(result.data!.userToken!);
  //           return result;
  //         } else {
  //           showToast('获取失败');
  //           return null;
  //         }
  //       },
  //     );
  //   } catch (e) {
  //     debugPrint(e.toString());
  //   }
  // }
}
